package cn.kinggm520.controller;

import cn.kinggm520.domain.Goods;
import cn.kinggm520.domain.Seller;
import cn.kinggm520.entity.Result;
import cn.kinggm520.feign.GoodsFeignClient;
import cn.kinggm520.feign.SearchFeignClient;
import cn.kinggm520.service.SellerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 作者: kinggm Email:731586355@qq.com
 * 时间:  2020-05-22 11:31
 */

@RestController
@RequestMapping("/seller")
@RefreshScope // 开启刷新功能
public class SellerController {

    @Autowired
    private GoodsFeignClient goodsFeignClient;

    @Autowired
    private SearchFeignClient searchFeignClient;

    @Autowired
    private SellerService sellerService;

    @PostMapping("/add")
    public Result addSeller(@RequestBody Seller seller) {
        System.out.println(seller);

        sellerService.addSeller(seller);
        return new Result(true, "添加成功", seller);
    }


    @GetMapping("/findById/{id}")
    public Result addSeller(@PathVariable("id") Integer id) {
        System.out.println(id);
        Seller seller = sellerService.findById(id);
        return new Result(true, "查询成功", seller);

    }


    @PostMapping("/addGoods/{sellerId}")
    public Result addGoods(@RequestBody Goods goods, @PathVariable("sellerId") Integer sellerId) {

        Seller seller = sellerService.findById(sellerId);
        goods.setSeller(seller.getSeller());
        goods.setCompany(seller.getCompany());
        goods.setCreateTime(new Date());
        Result result = goodsFeignClient.addGoods(goods);
        return result;
    }


    @PostMapping("/search")
    public List<Goods> findGoods(@RequestBody Map<String, String> param) {
        List<Goods> goods = searchFeignClient.findGoods(param);
        return goods;
    }


}
